package cn.edu.gcu.giftems.action;

import cn.edu.gcu.giftems.pojos.Type;
import cn.edu.gcu.giftems.service.TypeService;
import cn.edu.gcu.giftems.utils.PageUtils;
import cn.edu.gcu.giftems.utils.export.TypeExportUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-25
 * @see
 */
public class TypeAction extends ActionSupport implements ModelDriven<Type> {

    private TypeService typeService =
            (TypeService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("typeService");
    private Type type = new Type();
    private List<Type> list;

    @Override
    public Type getModel() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public void setTypeService(TypeService typeService) {
        this.typeService = typeService;
    }

    public List<Type> getList() {
        return list;
    }

    public void setList(List<Type> list) {
        this.list = list;
    }

    /**
     * 分页查询类型的执行方法
     */
    private Integer currPage = 1;
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    public String findByPage(){
        PageUtils<Type> pageUtils = typeService.findByPage(currPage);
        // ActionContext.getContext().getValueStack().push(pageUtils);
        pageUtils.setCurrPage(currPage);
        ActionContext.getContext().put("pageUtils", pageUtils);
        //list = pageUtils.getList();
        return "success";
    }

    public List<Type> findAll() {
        return typeService.findAll();
    }

    /**
     * 添加类型的执行方法
     */
    public String save(){
        typeService.save(type);
        return this.findByPage();
    }

    /**
     * 更新类型信息的执行方法
     */
    public String update(){
        if (type.getId() > 0)
            typeService.update(type);
        return this.findByPage();
    }
    /**
     * 根据类型id删除类型
     */
    public String delete(){
        type = typeService.findById(type.getId());
        if (type != null)
            typeService.delete(type);
        return this.findByPage();
    }

    public void export()
    {
        TypeExportUtils Export=new TypeExportUtils();
        try {
            Export.export(typeService);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出数据有误");
        }
    }

    public String findByParameter() {
        String filter_name = ActionContext.getContext().getParameters().get("filter-name").toString();
        String simple_filter_value = ActionContext.getContext().getParameters().get("simple-filter-value").toString();
        PageUtils<Type> pageUtils = typeService.findByParameter(currPage, filter_name,simple_filter_value);
        ActionContext.getContext().put("pageUtils", pageUtils);
        return "success";
    }
}
